﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UFIDA.U9.Cust.YYD.PlugBE.Dtos;
using UFIDA.U9.Cust.YYD.PlugBE.Public;
using UFSoft.UBF.Business;

namespace UFIDA.U9.Cust.YYD.PlugBE.ItemMasterP
{
    public class AfterDeleted : UFSoft.UBF.Eventing.IEventSubscriber
    {
        public void Notify(params object[] args)
        {
            #region 从事件参数中取得当前业务实体													 
            if (args == null || args.Length == 0 || !(args[0] is UFSoft.UBF.Business.EntityEvent))
                return;
            BusinessEntity.EntityKey key = ((UFSoft.UBF.Business.EntityEvent)args[0]).EntityKey;
            if (key == null)
                return;
            var holder = key.GetEntity() as UFIDA.U9.CBO.SCM.Item.ItemMaster;
            if (holder == null)
                return;
            #endregion
            HttpHelper httpHelper = new HttpHelper();
            JsonConfig config = new JsonConfig();
            httpHelper.GetMESToken();
            var URL = config.SelectToken("MES.URL") + "/mes_api/bzcomponent/u9c_data_download/MaterialInfoDataSave";

            MESDelItemMasterDTO mESDelItemMasterDTO = new MESDelItemMasterDTO();
            mESDelItemMasterDTO.material = holder.Code;

            var json = JsonConvert.SerializeObject(mESDelItemMasterDTO);

            //var result = httpHelper.Request("PUT");
            httpHelper.URL = URL;
            httpHelper.Content = json;
            var result = JsonConvert.DeserializeObject<MESReturnDTO>(httpHelper.HttpPosts( "PUT"));
            if (result.code != "0")
            {
                throw new Exception("同步MES失败。报错信息:" + result.message);
            }
        }
    }
}
